package alog

import (
	"a-common/config"
	"io"
	"log/slog"
	"net/http"
	"strings"
)

// SendPost 日志查询(大数据？)
func SendPost(path string, data string) ([]byte, error) {
	conf := config.Config.Openobserve
	req, err := http.NewRequest("POST", conf.BaseUrl+path, strings.NewReader(data))
	if err != nil {
		return nil, err
	}
	req.SetBasicAuth(conf.User, conf.Pwd)
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			slog.Error(err.Error())
		}
	}(resp.Body)
	// log.Println(resp.StatusCode)
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	return body, nil
}
